import numpy as np
Name after Rudolf Kálmán, the first well-know application of Kalman filter is estimating the trajectory of spaceship in Apollo project.
Simply speaking, the Kalman filter is an algorithm to estimate unobserved variable based on what you can observe, or combine several sources of data subject to noises.
State Space Model
For MA, you have to specify number of lags, the longer the lags the smoother the trend, but not an accurate estimate of most recent status.
In state space model, parameters can adapt over time. To use the cointegration example, hedge ratio \(\beta\) is state that evolves over time, due to market noises, we are unable to directly observe it.
The goal state space model is to infer the states as new information comes in. The Kalman filter is the most representative algorithm.
Bayesian is a natural framework for SS model.
Since the states of the system are time-dependent, we need to subscript them with . We will use \(\theta_t\) to represent a column vector of the states. State equation \[ \theta_t = G_t \theta_{t-1}+w_t \]
Another equation is \[ y_t=F_t^T \theta_t+v_t \] where \(y_t\) is observation and \(v_t\) is measurement error.
To fully specify the model \[ \begin{aligned} \theta_0 & \sim \mathcal{N}\left(m_0, C_0\right) \\ v_t & \sim \mathcal{N}\left(0, V_t\right) \\ w_t & \sim \mathcal{N}\left(0, W_t\right) \end{aligned} \]
How to solve it? Kalman filter is the solution.
Prior \[ \theta_t|D_{t-1}\sim N(a_t, R_t) \] Likelihood \[ y_t |\theta_t \sim N(F_t^T\theta_t, V_t) \] Posterior \[ \theta_t | D_t \sim N(m_t, C_t) \]
You can introduce a rolling window looking back for a certain period to update hedge ratio, in this way you estimate linear regression whenever a new bar comes out.
Kalman filter could help us adjust hedge ratio estimation over time. We treat hedge ratio \(\beta\) an observable status, the observable is the prices.
This is how you formulate a linear regression into State-Space model \[ y_t = F_t X_t + v_t =[\beta_0 \quad \beta_1]\ \begin{bmatrix} 1 \\ x_t \end{bmatrix} + v_t \]
The Kalman Filter is used to dynamically track the hedging ratio between the two in order to keep the spread stationary (and hence mean reverting).
= 1e-5
delta = delta / (1 - delta) * np.eye(2) trans_cov
trans_cov
array([[1.00001e-05, 0.00000e+00],
[0.00000e+00, 1.00001e-05]])